package org.b3log;

import static org.b3log.common.Constants.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.b3log.common.AsyncImageLoader;
import org.b3log.task.LoadShowTask;
import org.b3log.view.GridViewAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ChannelActivity extends Activity {
	private GridView gridView;
	private AsyncImageLoader loader = AsyncImageLoader.getInstance();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setTheme(R.style.titleComplex);
		setContentView(R.layout.channel);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_complex);
		TextView textTitle = (TextView) this.findViewById(R.id.textTitle);
		Intent intent = getIntent();
		textTitle.setText(intent.getStringExtra("title"));
		JSONArray array = null;
		try {
			array = new JSONArray(intent.getStringExtra("data"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		gridView = (GridView) findViewById(R.id.gridviewChannel);
		GridViewAdapter adapter = new GridViewAdapter(array, this, loader);
		gridView.setAdapter(adapter);

		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				// Log.i("MainActivity", "Position:" + position);
				loadShow((String) v.getTag());
			}
		});
	}

	public void loadShow(String href) {
		try {
			LoadShowTask task = new LoadShowTask(ChannelActivity.this);
			task.execute(DOMAIN + SHOW + URLEncoder.encode(href, CHARSET_ENCODING));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

}